﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Economo.UI.Views;
using Economo.UI.AccountsServiceReference;
using Economo.Services.BusinessEntities;

namespace Economo.UI.Presenters
{
    public class ManageAccountTypesPresenter : ManageEntitiesPresenter 
    {
        public ManageAccountTypesPresenter(IManageAccountTypesView view) : base(view)
        {

        }
        private IManageAccountTypesView AccountTypesView
        {
            get
            {
                return (IManageAccountTypesView)View;
            }
        }

        #region CRUD Methods
        protected override void UpdateEntity(int rowIndex)
        {
            AccountsServiceClient client = new AccountsServiceClient();
            try
            {
                Economo.Services.BusinessEntities.AccountType updatedAccountType= GetAccountTypeFromGridRow(rowIndex);

                client.UpdateAccountType(new UpdateAccountTypeRequest(updatedAccountType));
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
            finally
            {
                client.Close();
            }
        }
        protected override void DeleteEntity(object id)
        {
            AccountsServiceClient client = new AccountsServiceClient();
            try
            {
                byte accountTypeId = Convert.ToByte(id);
                client.DeleteAccountTypeById(new DeleteAccountTypeByIdRequest(accountTypeId));
                List<AccountType> accountTypes = (List<AccountType>)View.EntitiesList;
                AccountType accountType = (from b in accountTypes
                             where (b.Id == accountTypeId)
                                           select b).FirstOrDefault<AccountType>();
                accountTypes.Remove(accountType);
                View.EntitiesList = accountTypes;
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
            finally
            {
                client.Close();
            }
        }
        protected override void AddNewEntity()
        {
            List<AccountType> accountTypes = (List<AccountType>)View.EntitiesList;
            AccountsServiceClient client = new AccountsServiceClient();
            try
            {
                AccountType newAccountType = new AccountType();
                newAccountType.Description = "(replace)";
                newAccountType.Id = client.InsertAccountType(new InsertAccountTypeRequest(newAccountType)).AccountTypeId;
                accountTypes.Add(newAccountType);

                View.EntitiesList = accountTypes;
            }
            finally
            {
                client.Close();
            }
        }

        #endregion

        #region Side Methods
        private Economo.Services.BusinessEntities.AccountType GetAccountTypeFromGridRow(Int32 rowIndex)
        {
            Economo.Services.BusinessEntities.AccountType accountTypeOnGridRow = new Economo.Services.BusinessEntities.AccountType();
            accountTypeOnGridRow.Id = Convert.ToByte(View.GetEntityIdFromGridRow(rowIndex));
            accountTypeOnGridRow.Description = AccountTypesView.GetAccountTypeDescriptionFromGridRow(rowIndex);
            accountTypeOnGridRow.IsIncome = AccountTypesView.GetAccountTypeIsIncomeFromGridRow(rowIndex);
            return accountTypeOnGridRow;
        }
        #endregion

    }
}
